package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartVoSerivce;
import com.dongdongshop.uitl.CookieUtils;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    //注入service
    @DubboReference(timeout = 100000)
    private CartVoSerivce cartVoSerivce;

    //添加购物车
    @RequestMapping("addcartList")
    public String addcartList(HttpServletRequest request, Long itemId, Integer num, HttpServletResponse response) {
       //获取当前登录对象
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //获取当前购物车数据
        Result<List<CartVo>> listResult = getcartList(request, response);
        List<CartVo> data = listResult.getData();
        data = cartVoSerivce.addaddcartList(data, itemId, num);
        if (tbUser == null) {
            CookieUtils.setCookie(request, response, "cartList", JSONUtil.toJsonStr(data), true);
        } else {
            cartVoSerivce.addcartListredis(tbUser.getId(), data);
        }
        return "success-cart";
    }

    //获取购物车
    @ResponseBody
    @RequestMapping("getcartList")
    public Result<List<CartVo>> getcartList(HttpServletRequest request, HttpServletResponse response) {
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (tbUser == null) {
            String cookie = getCookie(request);
            if (StrUtil.isBlank(cookie)) {
                cookie = "[]";
            }
            List<CartVo> cartVos = JSONUtil.toList(cookie, CartVo.class);
            return Result.OK().setData(cartVos);
        } else {
            //获取redis
            List<CartVo> cartVos = cartVoSerivce.getcartVos(tbUser.getId());
            //获取cookie
            String cookie = getCookie(request);
            if (StrUtil.isBlank(cookie)) {
                cookie = "[]";
            }
            List<CartVo> cartVos1 = JSONUtil.toList(cookie, CartVo.class);
            if (cartVos1.size() != 0) {
                //合并redis和cookie里面的数据
                List<CartVo> list = cartVoSerivce.hebing(cartVos, cartVos1);
                //保存到redis
                cartVoSerivce.addcartListredis(tbUser.getId(), list);
                //删除cookie
                CookieUtils.deleteCookie(request, response, "cartList");
                return Result.OK().setData(list);
            }
            return Result.OK().setData(cartVos);
        }
    }

    //添加Cookie
    @ResponseBody
    @RequestMapping("addCookie")
    public String addCookie(HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.setCookie(request, response, "cartList", "dfdsfds", true);
        return "ok";
    }

    //获取Cookie
    @ResponseBody
    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest request) {
        String cartList = CookieUtils.getCookieValue(request, "cartList", true);
        if (StrUtil.isBlank(cartList)) {
            cartList = "[]";
        }
        return cartList;
    }

    //删除Cookie
    @ResponseBody
    @RequestMapping("deleteCookie")
    public String deleteCookie(HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.deleteCookie(request, response, "cartList");
        return "ok";
    }
}
